package com.budwk.app.miniapp.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import com.budwk.app.miniapp.models.WxUserRelation;
import com.budwk.app.miniapp.services.WxuserrelationService;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.nutz.lang.Strings;
import com.budwk.starter.common.openapi.annotation.*;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;

@IocBean
@At("/platform/miniapp/user/relation")
@ApiDefinition(tag = "微信小程序-用户关系")
@Slf4j
public class WxuserrelationController {
    @Inject
    private WxuserrelationService wxUserRelationService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询WxUserRelation")
    @ApiFormParams(
            value = {
                    @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
                    @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
                    @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
                    @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
            },
            implementation = WxUserRelation.class
    )
    @ApiResponses(
            implementation = WxUserRelation.class
    )
    @SaCheckLogin
    public Result<?> list(@Param("..") WxUserRelation bean, @Param("pageNo") int pageNo, @Param("pageSize") int pageSize, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }

        if (bean.getUserId() != null && Strings.isNotBlank(bean.getUserId().toString())) {
            cnd.and("user_id", "=", bean.getUserId());
        }

        if (bean.getOpenId() != null && Strings.isNotBlank(bean.getOpenId().toString())) {
            cnd.and("open_id", "=", bean.getOpenId());
        }

        if (bean.getAppType() != null && Strings.isNotBlank(bean.getAppType().toString())) {
            cnd.and("app_type", "=", bean.getAppType());
        }

        return Result.data(wxUserRelationService.listPage(pageNo, pageSize, cnd));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "新增WxUserRelation")
    @ApiFormParams(
            implementation = WxUserRelation.class
    )
    @ApiResponses
    @SLog(tag = "新增WxUserRelation", value = "${args[0].id}")
    @SaCheckLogin
    public Result<?> create(@Param("..") WxUserRelation wxUserRelation, HttpServletRequest req) {
        wxUserRelation.setCreatedBy(SecurityUtil.getUserId());
        wxUserRelationService.insert(wxUserRelation);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改WxUserRelation")
    @ApiFormParams(
            implementation = WxUserRelation.class
    )
    @ApiResponses
    @SLog(tag = "修改WxUserRelation", value = "${args[0].id}")
    @SaCheckLogin
    public Result<?> update(@Param("..") WxUserRelation wxUserRelation, HttpServletRequest req) {
        wxUserRelation.setUpdatedBy(SecurityUtil.getUserId());
        wxUserRelationService.updateIgnoreNull(wxUserRelation);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取WxUserRelation")
    @ApiFormParams(
            {
                    @ApiFormParam(name = "id", description = "唯一标识", required = true, check = true)
            }
    )
    @SaCheckLogin
    public Result<?> getData(@Param("id") String id, HttpServletRequest req) {
        WxUserRelation wxUserRelation = wxUserRelationService.fetch(id);
        if (wxUserRelation == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(wxUserRelation);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除WxUserRelation")
    @ApiFormParams(
            {
                    @ApiFormParam(name = "id", description = "唯一标识", required = true, check = true)
            }
    )
    @SLog(tag = "删除WxUserRelation", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> delete(@Param("id") String id, HttpServletRequest req) {
        WxUserRelation wxUserRelation = wxUserRelationService.fetch(id);
        if (wxUserRelation == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        wxUserRelationService.delete(id);
        req.setAttribute("_slog_msg", wxUserRelation.getId());
        return Result.success();
    }
}
